package com.sinovatio.dbserver.common;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Response<T> {

    private int code;
    private String message;
    private T data;

    private Response(int code) {
        this(code, ResultCode.getMessage(code));
    }

    private Response(T data) {
        this(ResultCode.SUCCESS, data);
    }

    private Response(int code, T data) {
        this(code, ResultCode.getMessage(code), data);
    }

    private Response(int code, String message) {
        this(code, message, null);
    }

    public static <T> Response<T> success() {
        return new Response<>(ResultCode.SUCCESS);
    }

    public static <T> Response<T> success(T data) {
        return new Response<>(data);
    }

    public static <T> Response<T> error(BaseException e) {
        return new Response<>(e.getCode(), e.getMessage());
    }

    public static <T> Response<T> error(int code) {
        return new Response<>(code);
    }

    public static <T> Response<T> error(int code, String message) {
        return new Response<>(code, message);
    }
}